package com.ks3.demo.main;

import android.util.Log;

import com.ksyun.ks3.model.result.GetObjectResult;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class Utils {
    public static void saveToLocal (File file, GetObjectResult response) {
        InputStream inputStream = response.getObject().getObjectContent();
        long length = response.getObject().getObjectMetadata().getContentLength();
        if (length > 0) {
            byte[] buffer = new byte[(int)length];
            int readCount = 0;
            while (readCount < length) {
                try{
                    readCount += inputStream.read(buffer, readCount, (int) length - readCount);
                }catch (Exception e){
                    Log.e("DownloadActivity", e.toString());
                }
            }
            try {
                FileOutputStream fout = new FileOutputStream(file);
                fout.write(buffer);
                fout.close();
            } catch (Exception e) {
                Log.e("DownloadActivity", e.toString());
            }
        }
    }
}
